﻿@using Microsoft.AspNetCore.Builder;
@using Microsoft.Extensions.Options;
@using SimpleIdServer.CredentialIssuer.Domains;
@using System.Globalization;
@inject DialogService dialogService
@inject IOptions<RequestLocalizationOptions> requestLocalizationOptions

<RadzenRow>    
    <RadzenColumn>        
        <RadzenStack Gap="1rem">
            <!-- Locale -->
            <RadzenFormField Text="@Global.Locale" Variant=Variant.Outlined>
                <ChildContent>
                    <RadzenDropDown Class="w-100"
                                    Data=@SupportedCultures
                                    TValue="CultureInfo"
                                    @bind-Value=@CurrentCulture
                                    TextProperty="DisplayName" />
                </ChildContent>
                <Helper>
                    <RadzenText TextStyle="TextStyle.Caption">@Global.LocaleHelper</RadzenText>
                </Helper>
            </RadzenFormField>
            <!-- Name -->
            <RadzenFormField Text="@Global.Name" Variant=Variant.Outlined>
                <ChildContent>
                    <RadzenTextBox @bind-Value=@Display.Name></RadzenTextBox>
                </ChildContent>
                <Helper>
                    <RadzenText TextStyle="TextStyle.Caption">@Global.NameHelper</RadzenText>
                </Helper>
            </RadzenFormField>
            <!-- Description -->
            <RadzenFormField Text="@Global.Description" Variant=Variant.Outlined>
                <ChildContent>
                    <RadzenTextBox @bind-Value=@Display.Description></RadzenTextBox>
                </ChildContent>
                <Helper>
                    <RadzenText TextStyle="TextStyle.Caption">@Global.DescriptionHelper</RadzenText>
                </Helper>
            </RadzenFormField>
            <!-- Text color -->
            <RadzenFormField Text="@Global.TextColor" Variant=Variant.Outlined>
                <ChildContent>
                    <RadzenColorPicker @bind-Value=@Display.TextColor ShowHSV=true ShowRGBA=true ShowColors=true ShowButton=false />
                </ChildContent>
                <Helper>
                    <RadzenText TextStyle="TextStyle.Caption">@Global.TextColorHelper</RadzenText>
                </Helper>
            </RadzenFormField>
            <!-- Background color -->
            <RadzenFormField Text="@Global.BackgroundColor" Variant=Variant.Outlined>
                <ChildContent>
                    <RadzenColorPicker @bind-Value=@Display.BackgroundColor ShowHSV=true ShowRGBA=true ShowColors=true ShowButton=false />
                </ChildContent>
                <Helper>
                    <RadzenText TextStyle="TextStyle.Caption">@Global.BackgroundColorHelper</RadzenText>
                </Helper>
            </RadzenFormField>
            <!-- Picture URL -->
            <RadzenImage Path="@Display.LogoUrl" Style="max-width: 60px; max-height: 60px;"></RadzenImage>
            <RadzenFormField Text="@Global.LogoUrl" Variant=Variant.Outlined>
                <ChildContent>
                    <RadzenTextBox @bind-Value=@Display.LogoUrl></RadzenTextBox>
                </ChildContent>
                <Helper>
                    <RadzenText TextStyle="TextStyle.Caption">@Global.LogoUrlHelper</RadzenText>
                </Helper>
            </RadzenFormField>
            <!-- Picture alt text -->
            <RadzenFormField Text="@Global.LogoAltText" Variant=Variant.Outlined>
                <ChildContent>
                    <RadzenTextBox @bind-Value=@Display.LogoAltText></RadzenTextBox>
                </ChildContent>
                <Helper>
                    <RadzenText TextStyle="TextStyle.Caption">@Global.LogoAltTextHelper</RadzenText>
                </Helper>
            </RadzenFormField>
            <div>
                <RadzenButton Click=@((evt) => Save()) Variant="Variant.Text">@Global.Save</RadzenButton>
            </div>
        </RadzenStack>
    </RadzenColumn>
    <RadzenColumn>
        <RadzenText TextStyle="TextStyle.H5">@Global.Preview</RadzenText>
        <CredentialViewer ServerId="@ServerId" Display="@Display"></CredentialViewer>
    </RadzenColumn>
</RadzenRow>

@code {
    [Parameter]
    public CredentialConfigurationTranslation Display { get; set; }
    [Parameter]
    public string ServerId { get; set; }

    CultureInfo CurrentCulture
    {
        get
        {
            if (Display != null && !string.IsNullOrWhiteSpace(Display.Locale)) return new CultureInfo(Display.Locale);
            return requestLocalizationOptions.Value.DefaultRequestCulture.Culture;
        }
        set
        {
            Display.Locale = value.ToString();
        }
    }

    IList<CultureInfo> SupportedCultures => requestLocalizationOptions.Value.SupportedCultures;

    void Save()
    {
        dialogService.Close(Display);
    }
}